//
//  UUIntentOrderBidVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 07/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUIntentOrderBidVC.h"
#import "UUIntentOrderManager.h"
#import "UUContentContainerVC.h"
#import "UUIntentOrderDetailBasicInfoVC.h"
#import <UUFoundation/DKInputTextField.h>
#import <UUFoundation/DKControlToolBar.h>

@interface UUIntentOrderBidActionVC : UUBaseVC <DKContentContainerProtocol>

@property (nonatomic, strong) NSNumber *intentOrderId;
@property (nonatomic, strong) DKInputTextField *textField;

@end

@implementation UUIntentOrderBidActionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textField = [DKInputTextField new];
    self.textField.leftMargin = 10;
    self.textField.keyboardType = UIKeyboardTypeDecimalPad;
    self.textField.font = [UIFont systemFontOfSize:15];
    self.textField.placeholder = @"请输入报价价格";
    self.textField.layer.borderWidth = 1;
    self.textField.layer.borderColor = COMMON_TEXT_LIGHT_COLOR.CGColor;
    self.textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-20);
    }];
    
    WEAK_SELF();
    DKControlToolBar *controlView = [DKControlToolBar new];
    controlView.barTintColor = [UIColor whiteColor];
    [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
    }];
    [controlView setDoneBlock:^{
        [weakSelf confirm];
    }];
    [controlView setDismissBlock:^{
        [weakSelf.textField resignFirstResponder];
    }];

    self.textField.inputAccessoryView = controlView;
    
    self.preferredContentSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
}

- (void)confirm {
    [self.textField resignFirstResponder];
    
    if ([self.textField.text isNumber]) {
        ShowIndicatorInKeyWindow();
        WEAK_SELF();
        [GetIntentOrderManager() asyncUpdateIntentOrderWithOrderId:self.intentOrderId
                                                             price:@([self.textField.text doubleValue] * 100)
                                                        actionType:UUIntentOrderActionTypeBid
                                                     completeBlock:^(NSError *error) {
                                                         HideIndicatorInKeyWindow()
                                                         
                                                         if (error == nil) {
                                                             [weakSelf dismissViewControllerAnimated:YES completion:nil];
                                                         } else {
                                                             ShowTipsInKeyWindow(error.localizedDescription);
                                                         }
                                                     }];
    } else {
        ShowTips(@"请输入正确的报价");
    }
}

@end

////////////////////////////////////////////////////////////////////////////////////

@interface UUIntentOrderBidVC ()

@property (nonatomic, strong) UUIntentOrderDetailModel *order;
@property (nonatomic, strong) UUContentContainerVC *contentContainerVC;

@end

@implementation UUIntentOrderBidVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"返回"
                                                                             target:self
                                                                           selector:@selector(back)];
    
    DKAssert(self.intentOrderId != nil);
    
    self.contentContainerVC = [UUContentContainerVC instanceOfAppropriate];
    [self addChildViewController:self.contentContainerVC];
    [self.view addSubview:self.contentContainerVC.view];
    [self.contentContainerVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    ShowIndicator();
    WEAK_SELF();
    [GetIntentOrderManager() asyncFetchIntentOrderDetailWithOrderId:self.intentOrderId
                                                      completeBlock:^(UUIntentOrderDetailModel *orderDetail, NSError *error) {
                                                          HideTipsOnView(weakSelf.view);
                                                          
                                                          if (error == nil) {
                                                              weakSelf.order = orderDetail;
                                                              [weakSelf.contentContainerVC removeAllChildrenVC];
                                                              [weakSelf setupUI];
                                                          } else {
                                                              ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                          }
                                                      }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupUI {
    UUIntentOrderDetailBasicInfoVC *basicInfoVC = [UUIntentOrderDetailBasicInfoVC instanceOfAppropriate];
    basicInfoVC.order = self.order;
    [self.contentContainerVC insertChildVC:basicInfoVC];
    
    UUIntentOrderBidActionVC *actionVC = [UUIntentOrderBidActionVC instanceOfAppropriate];
    actionVC.intentOrderId = self.intentOrderId;
    [self.contentContainerVC insertChildVC:actionVC];
}

@end
